package com.liuzh.kaoyanspringboot.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzh.kaoyanspringboot.entity.Article;
import com.liuzh.kaoyanspringboot.mapper.ArticleMapper;
import com.liuzh.kaoyanspringboot.service.ArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-03-10
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Page<Article> findPage(Page<Article> page, String name, Integer userId) {
//        // 1.从缓存获取数据
//        String jsonStr = stringRedisTemplate.opsForValue().get(Constants.ARTICLES_KEY);
//        Page<Article> res;
//        if(StrUtil.isBlank(jsonStr)) { // 2.当没有缓存，Json 为空
//            // 3.从数据库查询
//            res = articleMapper.findPage(page, name, userId);
//            // 缓存到 redis
//            stringRedisTemplate.opsForValue().set(Constants.ARTICLES_KEY, JSONUtil.toJsonStr(page));
//        } else {
//            res = JSONUtil.toBean(jsonStr, new TypeReference<Page<Article>>() {
//            }, true);
//        }
        return articleMapper.findPage(page, name, userId);
    }
    @Override
    public Page<Article> findCollectPage(Page<Article> page, Integer collectId, String articleName) {
        return articleMapper.findCollectPage(page, collectId, articleName);
    }

    @Override
    public Integer myCount() {
        return articleMapper.myCount();
    }

    @Override
    public List<Article> listDate() {
        return articleMapper.selectListDate();
    }

    @Override
    public List<Article> findSlideshow() {
        return articleMapper.selectSlideshow();
    }

    @Override
    public Article findById(Integer id) {
        return articleMapper.findById(id);
    }

    @Override
    public List<Article> findAll(Integer userId) {
        return articleMapper.findAll(userId);
    }

    @Override
    public boolean decrementLike(Integer id) {
        return articleMapper.decrementLike(id);
    }

    @Override
    public boolean increaseLike(Integer id) {
        return articleMapper.increaseLike(id);
    }

    @Override
    public boolean reNewById(Article article) {
        return articleMapper.reNewById(article);

    }

    @Override
    public Article miniFindById(Integer articleId) {
        return articleMapper.miniFindById(articleId);
    }

    @Override
    public boolean decrementCollect(Integer id) {
        return articleMapper.decrementCollect(id);
    }

    @Override
    public boolean increaseCollect(Integer id) {
        return articleMapper.increaseCollect(id);
    }

}
